var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';

// import { $wuxRefresher } from '../../components/wux';
import { $wuxPrompt } from '../../components/wux';


var app = getApp();
var that;

let  pageInex = 1;

Page({
  data: { 
    user:null,
    list : [],
    isLoadMore : false,
    isEnd : false,
    isEmpty:false
      ,inputShowed: false,
      inputVal: ""
      ,tid:0
  },
  onPullDownRefresh:function(){
    pageInex = 1;
    this.start=true;
    that.setData({
      list : [],
      isLoadMore : false,
      isEnd : false,
      isEmpty:false
    });
    util.showLoading();
    that.loadData();
  },
  onShow:function(){
    

    
  },
  onLoad: function (query) {
      that = this;

      that.data._sdesc="D几名";
    wx.setNavigationBarTitle( { title: that.data._sdesc });

    $wuxPrompt.init('msg1', {
      title: '暂时没有相关数据', 
    }).show()

    this.query = query;
    if( !util.judge.isNull(query.tid) ){
        that.data.tid=query.tid;
    }
    pageInex = 1;
    util.showLoading();
    app.getUserInfo()
    .then(res => {
        that.setData({
            user: res
        })
        that.loadData();
    });
   

  },
 
  onReady : function(){
    

  },
  onShareAppMessage: function () {
      return app.getShare();
  },
  onReachBottom : function(e,a){
      //加载更多
      if(!that.data.isEnd && !that.data.isLoadMore && !that.data.isEmpty){
          that.loadData();
      }
  },
  
  onForwardColumn : function(){
      wx.navigateTo({
        url : "../column/column"
      });
  },

  onPreImage : function(e){
      let src = e.currentTarget.dataset.imgSrc;
      Util.UI.preImage(src);
  },
  loadData: function(){

    that.setData({
      isLoadMore : true
    });

    var url = config.url_get_user_qd_list;
    var params = {
      pageIndex: pageInex,
      pageSize: app.globalData.defaultPageSize,
      code:that.data.inputVal,
      tid :that.data.tid
    }

    wxRequest.postRequest(url, params)
    .then(res=>{
      
      // if(that.refresher.start==true){
      //   that.refresher.start=false;
      //   that.refresher.events.emit(`scroll.refreshComplete`);
      // }

      if(pageInex==1 && res.length==0){
          that.setData({
            isLoadMore : false,
            isEnd : false,
            isEmpty :true,
          });
          return;
      }
      
      if(res.length > 0){
        pageInex++;

        for(var i in res){
          var now = new Date().getTime();
          //判断
          var status = 0;
          var str="未开始";
          res[i].credate = util.date2str(res[i].credate,"yyyy-MM-dd");
          
        }
        var data = that.data.list.concat(res);   
        that.setData({
            list : data,
            isLoadMore : false
        });
      }
      
      if(res.length < app.globalData.defaultPageSize || res.length == 0 ){
        that.setData({
          isLoadMore : false,
          isEnd : true
        });
      }

    })
    .catch(res => {
      util.alert(res.message);
    })
    .finally(function (res) {
      util.hideLoading();
      wx.stopPullDownRefresh();
    });
  },
  onrow:function(e){
    var data = e.currentTarget.dataset;
    util.confirm("确定要领奖吗?",function(res){
        if (res.confirm) {
            util.showLoading();
            var url = config.url_get_user_qdlj;
            var params = {
                qdid:data.id
            }
            wxRequest.postRequest(url, params)
            .then(res => {
                if(res==1){
                    that.onPullDownRefresh();
                    throw util.stop();
                }else{
                    util.alert("操作失败");
                }
                
            })
            .catch(res => {
                util.alert_catch(res.message);
            })
            .finally(function (res) {
                util.hideLoading();
            });
        }
    });

   
    
  },
  onedit:function(e){

      var url = config.url_user_instm;
      var params = {
          fid:e.detail.formId
      }

      wxRequest.postRequest(url, params)
      .then(res=>{
      });

      wx.navigateTo({
          url: '../team/edit?type=0&tid=0'
      })
  }
    ,oneditjs:function(e){

        var url = config.url_user_instm;
        var params = {
            fid:e.detail.formId
        }

        wxRequest.postRequest(url, params)
        .then(res=>{
        });

        wx.navigateTo({
            url: '../team/edit?type=1&tid=0'
        })
    }

    ,showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    hideInput: function (obj) {
        //this.setData({
        //    inputVal: "",
        //    inputShowed: false
        //});
        setTimeout(that.onPullDownRefresh,200);
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
            ,inputShowed: false
        });
        //重新加载
        setTimeout(that.onPullDownRefresh,200);
    },
    inputTyping: function (e) {
        this.setData({
            inputVal: e.detail.value
        });
    }
})


